package cfg;

import util.*;

import java.util.List;
import java.util.Vector;

public class CoverageMatrixReader {
	public Arquivo coverageFile;
	public static final String classBeginningString = "CLASS_";
	public static final String methodBeginningString = "METHOD_";
	public static final String methodEndString = "ENDMETHOD_";
	public static final String classEndString = "ENDCLASS_";
	public static final String fileEndString = "ENDFILE_";
	
	public CoverageMatrixReader(Arquivo coverageMatrixFile){
		this.coverageFile = coverageMatrixFile;
	}
	
	public String removeStringFromString(String everything, String toBeRemoved){
		String retorno = "";
		int lengthToBeCut = 0;
		
		if(everything.contains(toBeRemoved)){
			if(toBeRemoved.equals(classBeginningString)){
				lengthToBeCut = 6;
			} else if(toBeRemoved.equals(methodBeginningString)){
				lengthToBeCut = 7;
			} else if(toBeRemoved.equals(methodEndString)){
				lengthToBeCut = 10;
			} else if(toBeRemoved.equals(classEndString)){
				lengthToBeCut = 9;
			} else if(toBeRemoved.equals(fileEndString)){
				lengthToBeCut = 8;
			}
			
			everything = everything.substring(lengthToBeCut, everything.length());
		}
		
		retorno = everything;
		return retorno;
	}
	
	public List<String> readCoverageFile(List<String> dangerousMethods){
		List<String> retorno = new Vector<String>();
		
		String class_ = (this.coverageFile.readLine()).trim();
		String className = this.removeStringFromString(class_, classBeginningString);
		
		while(!class_.equals("ENDFILE")){
			String method_= (this.coverageFile.readLine()).trim();
			String methodName = this.removeStringFromString(method_, methodBeginningString);
			
			while(!method_.substring(0, 8).equals("ENDCLASS")){
				//TODO:AQUI!
				String next = (this.coverageFile.readLine()).trim();
				String full_class_method_name = className + "." + methodName;
			
				while(!next.substring(0, 8).equals("ENDMETHO") && (next.endsWith("_covCase"))){
					if(dangerousMethods.contains(full_class_method_name)){
						retorno.add(next);
					}
					next = (this.coverageFile.readLine()).trim();
				}
				
				String blankSpace = this.coverageFile.readLine();
				
				method_= (this.coverageFile.readLine()).trim();
				methodName = this.removeStringFromString(method_, methodBeginningString);
			}
			String blankSpace = this.coverageFile.readLine();
			blankSpace = this.coverageFile.readLine();
			
			class_ = (this.coverageFile.readLine()).trim();
			className = this.removeStringFromString(class_, classBeginningString);
		}
		
		return retorno;
	}
	
}
